#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "HymanHu";

'''
Python Base 练习
'''

import os;
import pymysql;
from util.pymysql_util import *;
import json;
from base.python_base_03 import *;
from datetime import datetime;

print("======== 函数定义 ========");
def fun01():
    print("cdsacacsa");

print("======== 位置参数 ========");
def fun02(name, age=18, gender="male"):
    print(name, age, gender);

print("======== 可变参数 ========");
def fun03(*args):
    print(args);

print("======== 可变关键字参数 ========");
def fun04(**args):
    print(args);

print("======== 命名关键字参数 ========");
def fun05(name, *, age):
    print(name, age);

print("======== 组合参数 ========");

print("======== 空 ========");
def fun06():
    pass;

print("======== 函数返回值 ========");
def dun07():
    print("cdsadas");

def dun08():
    print("cdsadas");
    return "hujiang";

def dun09():
    print("cdsadas");
    return "hujiang", 18, "male";

print("======== 递归 ========");
def dun10(num):
    if num == 1:
        return num;

    return num * dun10(num - 1);

print("======== 高级特性 ========");
def dun11():
    l = list(range(1, 10));
    print(l[:5:2]);
    print(l[-5:]);

    l = [];
    # for i in range(1, 11):
    #     url = "http://www.sfac.xyz/account/user/%d" % (i,);
    #     l.append(url);
    # print(l);

    l = list("http://www.sfac.xyz/account/user/%d" % (index,) for index in range(1, 11));
    print(l);

print("======== 错误 ========");
def fun12():
    try:
        a = 1 / 0;
        print(a);
    except BaseException as e:
        print(e);
    finally:
        print("cdscdad");

print("======== 文件读写 ========");
def fun13(path):
    f = open(file=path, mode='a', encoding="utf-8");
    f.write("我叫胡江11111。");
    f.write("\n");
    f.write("cdcdacda11111");
    f.close();

def fun14(path):
    f = open(file=path, mode='r', encoding="utf-8");
    print(f.readlines());
    f.close();

def fun15(path1, path2):
    f1 = open(file=path1, mode='rb');
    content = f1.read();
    f2 = open(file=path2, mode='wb');
    f2.write(content);
    f1.flush();
    f1.close();
    f2.close();

def fun16(path):
    with open(file=path, mode='r', encoding="utf-8") as f:
        print(f.readlines());

print("======== os ========");
def fun17():
    print(os.name);
    # if not os.path.exists("/temp/hj/jh"):
    #     os.makedirs("/temp/hj/jh");

    os.makedirs("/temp/hj/jh", exist_ok=True);

print("======== db ========");
def fun18():
    connect,cursor = None, None;
    try:
        # 链接数据库：地址、端口、数据库名、用户名、密码、编码，返回链接对象
        connect = pymysql.connect(host="127.0.0.1", port=3306, database="kg20",
                        user="root", password="root", charset="utf8mb4");
        # 从链接对象中获取游标对象
        cursor = connect.cursor();
        # 用游标对象对数据库进行增删改查的操作
        # insert_sql = "insert into user(user_name, password, create_date) " \
        #              "values('manager', '111111', '2023-08-29 14:14:14')";
        # count = cursor.execute(insert_sql);
        # print(count);
        # 增删改，返回影响数据条目数，如果没有，提交事务，反之，回滚事务
        # connect.commit();
        # 查询操作，返回查询结果集
        query_sql = "select * from user";
        cursor.execute(query_sql);
        result = cursor.fetchall();
        print(result);
    except BaseException as e:
        print(e);
        connect.rollback();
    finally:
        # 关闭连接、关闭游标
        if cursor:
            cursor.close();
        if connect:
            connect.close();

def fun19(sql):
    result = execute_edit_(sql);
    print(result);

def fun20(sql):
    result = execute_query_(sql);
    print(result);

print("======== json ========");
def fun21():
    student = Student(
        id=1,
        name="hj",
        age=18,
        no=123
    );
    print(student.__dict__);
    print(json.dumps(student.__dict__));
    d = json.loads('{"id": 2, "name": "hj111", "age": 19, "no": 124}');
    student.__dict__ = d;
    print(student.id, student.name);

print("======== datetime ========");
def fun22():
    print(datetime.now());
    print(datetime.now().timestamp());
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"));
    print(datetime.strptime("2023-07-29 15:52:27", "%Y-%m-%d %H:%M:%S"));

if __name__ == '__main__':
    fun22();
